package ua.epam.muzalevskiy.traction.tags;

import java.io.IOException;
import java.util.Formatter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import static javax.servlet.jsp.tagext.Tag.SKIP_BODY;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.log4j.Logger;

/**
 * Current class describe header of Web application.
 *
 * @author Muzalevskiy Vitaliy
 */
public class Header extends TagSupport {
    private static Logger logger = Logger.getLogger(Header.class);

    @Override
    public int doStartTag() throws JspException {
        /*
         * defines Formatter object (formatter) to generate HTML code with
         * certain values of tags and attributes.  formatter appends every
         * generated string to a StringBuilder object (out)
         */
        StringBuilder out = new StringBuilder();
        Formatter formatter = new Formatter(out);
        try {

//                        <table align = "center" width = "1000">
//                            <tr>
            formatter.format("<table align =\"center\" width = \"1000\"><tr>");

//                        <td align ="left">
//                            <form action="./Controller" method="post" >
//                                <a href="Controller?command=null">To main page</a>
//                            </form>
//                        </td>
            formatter.format("<td align =\"left\"><form action=\"./Controller\" method=\"post\" >"
                    + "<a href=\"Controller?command=null\">%1$s</a></form></td> ",
                    pageContext.getSession().getAttribute("to_main_page"));

//                        <td align ="left">
//                            <form action="./Controller" method="post" >
//                                <a href="Controller?command=view_administratiom_page">
//                                    To administration page
//                                </a>
//                            </form>
//                       </td>
            formatter.format("<td align =\"left\"><form action=\"./Controller\" method=\"post\" >"
                    + "<a href=\"Controller?command=view_administratiom_page\">"
                    + "%1$s </a></form></td>", pageContext.getSession()
                    .getAttribute("to_administration_page"));
            
            if(pageContext.getSession().getAttribute("user_type") == null){

//                       <td align ="center">
//                            <form action="./Controller" method="post" >                                        
//                                 <input type="text" name="login" value="login"/>
//                                 <input type="text" name="password" value="password"/>
//                                 <input type="hidden" name="command" value="login"/>
//                                 <input type="submit" value="logon"/>
//                            </form>
//                       </td>
            formatter.format("<td align =\"center\"><form action=\"./Controller\" method=\"post\" >"
                    + "<input type=\"text\" name=\"login\" value=\"login\"/>"
                    + "<input type=\"password\" name=\"password\" value=\"password\"/>"
                    + "<input type=\"hidden\" name=\"command\" value=\"login\"/>"
                    + "<input type=\"submit\" value=\"logon\"/></form></td>");

//                      <td align ="left">
//                           <form action="./Controller" method="post" >
//                                <input type="hidden" name="command" 
//                                       value="view_new_user_page"/>
//                                <input type="submit" value="Registration"/>
//                           </form>
//                      </td>
            formatter.format("<td align =\"left\"><form action=\"./Controller\" method=\"post\" >"
                    + "<input type=\"hidden\" name=\"command\" value=\"view_new_user_page\"/>"
                    + "<input type=\"submit\" value= %1$s /></form></td>", 
                    pageContext.getSession().getAttribute("registration"));
            } else {

//                      <td align ="center">
//                           <form action="./Controller" method="post" >
//                                <c:out value="Вы зашли как ${sessionScope.user_name}  "/>
//                                <input type="hidden" name="command" value="logout"/>
//                                <input type="submit" value="logout"/>
//                           </form>
//                      </td> 
            formatter.format("<td align =\"center\"><form action=\"./Controller\" method=\"post\" >"
                    + "%1$s %2$s"
                    + "<input type=\"hidden\" name=\"command\" value=\"logout\"/>"
                    + "<input type=\"submit\" value=\"logout\"/></form></td>",
                    pageContext.getSession().getAttribute("entering_with_login"),
                    pageContext.getSession().getAttribute("user_name"));
            }

//                      <td align ="right">
//                           <form action="./Controller" method="post" >
//                                <a href="Controller?command=change_locale_en">En</a>
//                                <a href="Controller?command=change_locale_ru">Ru</a>
//                           </form>
//                      </td>
//                  </tr>
//             </table>
            formatter.format("<td align =\"right\"><form action=\"./Controller\" method=\"post\" >"
                    + "<a href=\"Controller?command=change_locale_en\">En</a>"
                    + "<a href=\"Controller?command=change_locale_ru\">Ru</a>"
                    + "</form></td>");
            pageContext.getOut().write(out.toString());           
        } catch (IOException e) {
            logger.error(e.getMessage());
            throw new JspTagException(e.getMessage());
        }
        return SKIP_BODY;
    }
}
